package com.tmall.testgen.client.runinspring;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;

/**
 * 
 * @author liuzx
 * 
 */
public class LaunchMainClass {
	

	public static void launch(String projectName, String mainClass) {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IProject project = workspace.getRoot().getProject(projectName);
		IJavaProject javaProject = JavaCore.create(project);
		String name = mainClass;
		try {
			launch(javaProject, name, mainClass);
		} catch (CoreException e) {
			e.printStackTrace();
		}
		
//		localJars.add("/testgenlib/hsf.app.spring-1.4.9.jar");
//		localJars.add("/testgenlib/hsf.lib-1.4.9.jar");
//		localJars.add("/testgenlib/hsf.services-1.4.9.jar");
//		localJars.add("/testgenlib/servlet-api-2.5.jar");
	}

	private static void launch(IJavaProject proj, String name, String mainClass)
			throws CoreException {
		try {
			ILaunchManager manager = DebugPlugin.getDefault()
					.getLaunchManager();
			ILaunchConfigurationType type = manager
					.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
			ILaunchConfiguration config = null;
			
			ILaunchConfiguration[] configurations = manager
					.getLaunchConfigurations(type);
			for (int i = 0; i < configurations.length; i++) {
				String projectName=proj.getProject().getName().toLowerCase();
				List<String> pathes= (List<String>) configurations[i].getWorkingCopy().getAttributes().get("org.eclipse.debug.core.MAPPED_RESOURCE_PATHS");
				if(pathes!=null&&pathes.size()>0){
					String mapPath=pathes.get(0);
					if(mapPath!=null&&mapPath.startsWith("/"))
						mapPath=mapPath.substring(1).toLowerCase();
					int index=mapPath.indexOf("/");
					if(index>0)
						mapPath=mapPath.substring(0, index);
					if (projectName.equals(mapPath)&&configurations[i].getName().equals(name)){
						
						config =configurations[i] ;
					}
				}
			}
			// else create a new one
			if (config == null) {
				ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
						name);
				wc.setAttribute(
						IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
						proj.getProject().getName());
				// current directory should be the project root
				wc.setAttribute(
						IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
						proj.getProject().getLocation().toString());
				// use the suplied args
				wc.setAttribute(
						IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
						mainClass);
				// wc.setAttribute(
				// IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
				// args);
				// saves the new config
				
				config = wc.doSave();
			}
			config.launch(ILaunchManager.DEBUG_MODE, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
